package GUI;
/**hofit**/
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import Client.BusinessImplements;

import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.JDialog;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.GregorianCalendar;
import javax.swing.border.LineBorder;

public class Add_a_Copy extends Abstract_Jpanel {

	/**
	 * in this panel you add a new copy of an existing Item by it's ID
	 */
	public JButton btnBack;
	private JTextField txtPrintDate;
	private JTextField txtPurchaseDate;
	private JButton btnGo;
	private JPanel panel;
	private String itemID;
	private JPanel panel_1;
	
	
	public Add_a_Copy() {
		/**
		 * being done when the user press add
		 * check all the fields if there not empty
		 * if one or more fields empty pop up message to the user
		 * if all the fields are not empty: call to the function addItemCopy from the server
		 * if the edit success tell to the user and empty the text box
		 * if the edit faild inform the user  
		 */
		btnGo = new JButton("Add");
		btnGo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String error=new String("");
				if (txtPrintDate.getText().equals(""))
					error+="\nmissing print date ";
				if (txtPurchaseDate.getText().equals(""))
					error+="\nmissing purchase date ";
				if (!error.equals(""))
					JOptionPane.showMessageDialog(null,"Apply faild "+error);
				else
					if (BusinessImplements.GetInstance().getServerProxy().addItemCopy(itemID, txtPrintDate.getText(),txtPurchaseDate.getText()).get(0).equals("true"))
						{
							JOptionPane.showMessageDialog(null,"The insert success");
							txtPrintDate.setText("");
							txtPurchaseDate.setText("");
						}
					else
						JOptionPane.showMessageDialog(null,"Error");
						
			}
		});
		btnGo.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnGo.setBounds(557, 345, 90, 37);
		
		
		 btnBack = new JButton("Back");
		 btnBack.setFont(new Font("Tahoma", Font.PLAIN, 14));
		 btnBack.setBounds(32, 345, 90, 37);
		 
		 
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 Finding_Item_To_Add_copy_Of_It add_copy_first=new Finding_Item_To_Add_copy_Of_It();
					Sign_in_panel sip=new Sign_in_panel();
					on_screen_unload();
					BusinessImplements.GetInstance().getMainFrame().setContentPane(add_copy_first);
					add_copy_first.setSize(getMaximumSize());
					add_copy_first.on_screen_load();
				
				
			}
		});
		setLayout(null);
		add(btnGo);
		add(btnBack);
		
		panel = new JPanel();
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(23, 59, 653, 275);
		add(panel);
		panel.setLayout(null);
		
		panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(SystemColor.control, 6));
		panel_1.setBackground(new Color(220, 220, 220));
		panel_1.setBounds(89, 42, 453, 180);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblBookName = new JLabel("print date:");
		lblBookName.setBounds(29, 49, 64, 17);
		panel_1.add(lblBookName);
		lblBookName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		
		txtPrintDate = new JTextField();
		txtPrintDate.setBounds(135, 49, 120, 20);
		panel_1.add(txtPrintDate);
		txtPrintDate.setEditable(false);
		txtPrintDate.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				
				   
				
			}
		});
		txtPrintDate.setColumns(10);
		
		JButton btnNewButton = new JButton("...");
		btnNewButton.setBounds(290, 48, 30, 23);
		panel_1.add(btnNewButton);
		
		JButton button = new JButton("...");
		button.setBounds(290, 100, 30, 23);
		panel_1.add(button);
		
		txtPurchaseDate = new JTextField();
		txtPurchaseDate.setBounds(135, 101, 120, 20);
		panel_1.add(txtPurchaseDate);
		txtPurchaseDate.setEditable(false);
		txtPurchaseDate.setColumns(10);
		
		JLabel lblBookId = new JLabel("purchase date: ");
		lblBookId.setBounds(29, 101, 96, 17);
		panel_1.add(lblBookId);
		lblBookId.setFont(new Font("Tahoma", Font.PLAIN, 14));
		button.addActionListener(new ActionListener() {

			/**
			 * open window that permit to choose date from calendar 
			 * and put the date in the filed
			 */
			public void actionPerformed(ActionEvent e) {
				// initial date
				   GregorianCalendar date = new GregorianCalendar();
				 
				       // The  owner is the JFrame of the application ("AppClass.this")
				 
				 // show the date chooser
				   DateChooser dc = new DateChooser(BusinessImplements.GetInstance().getMainFrame(), date);
				
				   // user can eiter choose a date or cancel by closing
				   if (dc.showDateChooser() == DateChooser.OK_OPTION) 
				   {
				     date = dc.getDate();
				     StringBuilder stringBuilder = new StringBuilder();
					stringBuilder.append(date.getTime().getYear()+1900);
					stringBuilder.append("-");
					stringBuilder.append(date.getTime().getMonth()+1);
					stringBuilder.append("-");
					stringBuilder.append(date.getTime().getDate());
					txtPurchaseDate.setText(stringBuilder.toString());
				     
				   }
			}
		});
		btnNewButton.addActionListener(new ActionListener() {
			
			/**
			 * open window that permit to choose date from calendar 
			 * and put the date in the filed
			 */
			public void actionPerformed(ActionEvent e) {
				// initial date
				   GregorianCalendar date = new GregorianCalendar();
				 
				       // The  owner is the JFrame of the application ("AppClass.this")
				 
				 // show the date chooser
				   DateChooser dc = new DateChooser(BusinessImplements.GetInstance().getMainFrame(), date);
				
				   // user can eiter choose a date or cancel by closing
				   if (dc.showDateChooser() == DateChooser.OK_OPTION) 
				   {
				     date = dc.getDate();
				     StringBuilder stringBuilder = new StringBuilder();
					stringBuilder.append(date.getTime().getYear()+1900);
					stringBuilder.append("-");
					stringBuilder.append(date.getTime().getMonth()+1);
					stringBuilder.append("-");
					stringBuilder.append(date.getTime().getDate());
					txtPrintDate.setText(stringBuilder.toString());
				     
				   }
			}
		});
		
		JLabel lblAddACopy = new JLabel("Add a copy");
		lblAddACopy.setForeground(new Color(25, 25, 112));
		lblAddACopy.setBounds(36, 11, 219, 37);
		add(lblAddACopy);
		lblAddACopy.setFont(new Font("MV Boli", Font.BOLD, 30));

	}
	public void setItemID(String id){
		itemID=id;
	}
	public void check(){
		
	}
	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void on_screen_load() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
}
